﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace RobotEscape.Entities
{
    public class SpriteEntity : EntityBase
    {
        #region props
        public ImageBrush imageBrush = null;
        TransformGroup transform = null;
        protected double lastFrameTime = 0;
        protected int currentFrame = 0;
        public bool doneAnimating = false;
        #endregion

        public SpriteEntity()
        {
            this.uiElement = new Rectangle();
            transform = new TransformGroup();
            transform.Children.Add(new TranslateTransform());
            transform.Children.Add(new ScaleTransform());
            imageBrush = new ImageBrush
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
            imageBrush.Transform = transform;
        }

        public override void Update(GameTime time)
        {
            base.Update(time);
        }

        public virtual void Update(GameTime time, Sprite sprite)
        {
            Rectangle rect = this.uiElement as Rectangle;
            Canvas.SetLeft(rect, this.PosX);
            Canvas.SetTop(rect, this.PosY);
            rect.Width = this.width;
            rect.Height = this.height;

            // animate sprite frames
            if (!doneAnimating)
                AnimateSprite(time, sprite);
        }

        internal void AnimateSprite(GameTime time, Sprite sprite)
        {
            var timeElapsed = time.TotalRealTime.TotalMilliseconds - lastFrameTime;
            if (timeElapsed >= (1 / sprite.fps) * 1000)
            {
                lastFrameTime = time.TotalRealTime.TotalMilliseconds;
                ++currentFrame;
                currentFrame %= sprite.frameCount;
                if (!sprite.loop && currentFrame == 0)
                {
                    doneAnimating = true;
                    return;
                }

                imageBrush.ImageSource = ResourceHelper.GetBitmap(sprite.file);
                (transform.Children[0] as TranslateTransform).X = -sprite.x - (sprite.width * this.currentFrame);
                (transform.Children[0] as TranslateTransform).Y = -sprite.y;
                if (sprite.flip)
                {
                    (transform.Children[1] as ScaleTransform).ScaleX = -1;
                    (transform.Children[0] as TranslateTransform).X -= sprite.width;
                }
                else
                {
                    (transform.Children[1] as ScaleTransform).ScaleX = 1;
                }
                Rectangle rect = this.uiElement as Rectangle;
                rect.Fill = imageBrush;
            }
        }

    }
}

